package com.itgaoshu.hospitalproject.mapper;

import com.itgaoshu.hospitalproject.bean.Lpharmacy;
import com.itgaoshu.hospitalproject.bean.Lrecord;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface LDrugMapper {
    //查询所有的药品
    List<Lpharmacy> selDrug(Lpharmacy lpharmacy);
    //查询该患者的所有药品清单
    @Select("select hospitalpriceid,registerid,durgname,durgnum,repiceprice,repicetotal,htime,state from hospitalprice where registerid=#{registerid} and state=#{state} order by htime desc ")
    List<Lrecord> selDrugs(Lrecord lrecord);

    //当患者没有这个药品时直接添加该患者的药品清单
    @Insert("insert into hospitalprice values(null,#{registerid},#{durgname},#{durgnum},#{repiceprice},#{repicetotal},now(),#{state})")
    int addDrugs(Lrecord lrecord);

    //当患者有这个药品清单时直接修改数量和总价即可
    @Update("update hospitalprice set durgnum = durgnum+#{durgnum},repicetotal=durgnum*repiceprice,htime=now() where durgname=#{durgname} and registerid=#{registerid} and state=#{state}")
    int updateLrecord(Lrecord lrecord);
    //修改药品库中的该药品的清单
    @Update("update pharmacy set drugstorenum=drugstorenum-#{durgnum} where pharmacyName=#{durgname}")
    int updateNum(Lrecord lrecord);

    //删除该患者的药品清单根据id来删除
    @Delete("delete from hospitalprice where hospitalpriceid=#{hospitalpriceid}")
    int delDrug(Lrecord lrecord);
    //当删除患者的药品清单时要添加该药品库房中的数量
    @Update("update pharmacy set drugstorenum=drugstorenum+#{durgnum} where pharmacyName=#{durgname}")
    int updNum(Lrecord lrecord);


    //取出药品
    @Update("update hospitalprice set state=2,htime=NOW() where state=0 and registerId=#{registerid}")
    int updItem(Lrecord lrecord);

}
